<?php
class Ms_kategori extends CI_Controller {

	function __construct()
	{
		parent::__construct();	
		$this->load->helper('flexigrid');
		$this->load->library('flexigrid');
		$this->load->library('form_validation');
		$this->load->model('kategori_model');
	}
	
	function index()
	{
		//ver lib
		
		/*
		 * 0 - display name
		 * 1 - width
		 * 2 - sortable
		 * 3 - align
		 * 4 - searchable (2 -> yes and default, 1 -> yes, 0 -> no.)
		 */
		$colModel['no'] = array('No',40,TRUE,'center',0);
		$colModel['NAMA_KATEGORI'] = array('Kategori',300,TRUE,'center',1);
		$colModel['UPLOAD'] = array('Daftar Upload File',100,FALSE,'center',0);
		$colModel['KETERANGAN'] = array('Keterangan',500,TRUE,'center',0);
		
		/*
		 * Aditional Parameters
		 */
		$gridParams = array(
		'width' => 'auto',
		'height' => '345',
		'rp' => 10,
		'rpOptions' => '[10,15,20,25,40]',
		'pagestat' => 'Displaying: {from} to {to} of {total} items.',
		'blockOpacity' => 0.5,
		'title' => 'Daftar Kategori'
		);
		
		/*
		 * 0 - display name
		 * 1 - bclass
		 * 2 - onpress
		 */
		$buttons[] = array('Add','add','org_js');
		$buttons[] = array('Delete','delete','org_js');
		$buttons[] = array('separator');
		$buttons[] = array('Select All','add','org_js');
		$buttons[] = array('Deselect All','delete','org_js');
		$buttons[] = array('separator');
		
		//Build js
		//View helpers/flexigrid_helper.php for more information about the params on this function
		$grid_js = build_grid_js('flex1',site_url("/ms_kategori/listkategori_grid/"),$colModel,'no','asc',$gridParams,$buttons);
		
		if($this->session->flashdata('suc_msg'))
			$data['header_info'] = '<div class="ret_success">'.$this->session->flashdata('suc_msg').'</div>';
		
		$data['title'] = "Contoh Flexigrid";
		$data['info'] = "Flexigrid";
		$data['js_grid'] = $grid_js;
		$data['version'] = "0.36";
		$data['download_file'] = "Flexigrid_CI_v0.36.rar";
		
		$data['added_js'] = 
			"<script type='text/javascript'>
			function org_js(com,grid)
			{
				if (com=='Select All')
				{
					$('.bDiv tbody tr',grid).addClass('trSelected');
				}
				
				if (com=='Deselect All')
				{
					$('.bDiv tbody tr',grid).removeClass('trSelected');
				}
				
				if (com=='Add')
				{
					location.href='".base_url()."index.php/ms_kategori/input_kategori/'; 
				}
				
				if (com=='Delete')
				{
				   if($('.trSelected',grid).length>0){
					   if(confirm('Anda yakin ingin menghapus ' + $('.trSelected',grid).length + ' buah data?')){
							var items = $('.trSelected',grid);
							var itemlist ='';
							for(i=0;i<items.length;i++){
								itemlist+= items[i].id.substr(3)+',';
							}
							$.ajax({
							   type: 'POST',
							   url: '".site_url('/ms_kategori/delete_kategori')."',
							   data: 'items='+itemlist,
							   success: function(data){
								$('#flex1').flexReload();
								alert(data);
							   }
							});
						}
					} else {
						return false;
					} 
				}          
			} 
			</script>
			";
		
		$data['content'] = $this->load->view('grid',$data,true);
		$this->load->view('main',$data);
	}
	
	function listkategori_grid()
	{
		$valid_fields = array('NAMA_KATEGORI');
		
		$this->flexigrid->validate_post('ID_KATEGORI','asc',$valid_fields);

		$records = $this->kategori_model->get_kategori();
		// var_dump($records);
		$this->output->set_header($this->config->item('json_header'));
		$no = 0;
		/*
		 * Json build WITH json_encode. If you do not have this function please read
		 * http://flexigrid.eyeviewdesign.com/index.php/flexigrid/example#s3 to know how to use the alternative
		 */
		foreach ($records['records']->result() as $row)
		{
			$record_items[] = array($row->ID_KATEGORI,
			$no+1,
			'<div align="center">'.$row->NAMA_KATEGORI.'</div>',
			'<a title="Input Aspek Penilaian pada Mata Pelajaran ini." href=\''.site_url().'/upload_surat/index/'.$row->ID_KATEGORI.'/\'><img border=\'0\' src=\''.base_url().'images/main/39.png\'></a> ',
			'<div align="center">'.$row->KETERANGAN.'</div>'	
			);
			$no++;
		}
		//Print please
		if(isset($record_items))
				$this->output->set_output($this->flexigrid->json_build($records['record_count'],$record_items));
			else
				$this->output->set_output('{"page":"1","total":"0","rows":[]}');
	}
	
	function cek_validasi() {	
			// Setting Rules
			$this->form_validation->set_rules('kategori', 'Nama kategori', 'required');
			
			// Setting Error Message
			$this->form_validation->set_message('required', 'Kolom %s harus diisi!!');
			
			// Setting Delimiter
			$this->form_validation->set_error_delimiters('<li class="error">', '</li>');
			return $this->form_validation->run();
	}
	
	function input_kategori()
    {
		if ($this->cek_validasi() == FALSE){
			$data['status'] = 'new';
			$data['content'] = $this->load->view('form_kategori',$data,true);
			$this->load->view('main',$data);
		}
		else{
			$this->insert_kategori();
		}            
    }
	
	function insert_kategori(){
		$data = array(
			'NAMA_KATEGORI'			=> $this->input->post('kategori'),
			'KETERANGAN'	 		=> $this->input->post('keterangan')
		);
		$this->kategori_model->insert_kategori($data);
		$this->session->set_flashdata('suc_msg', 'Data berhasil disimpan.');
		redirect('ms_kategori'); 
	}
	
	function edit_kategori($id)
    {
 		if ($this->cek_validasi() == FALSE){
			$data['status'] = 'edit';
			$data['rs_kategori'] = $this->pelajaran_model->get_kategoriID($id);
			$data['content'] = $this->load->view('form_kategori',$data,true);
			$this->load->view('main',$data);
		}
		else{
			$this->update_kategori($id);
		}            
    }
	
	function update_kategori($id){
		$data = array(
			'NAMA_KATEGORI'			=> $this->input->post('kategori'),	
			'KETERANGAN'	 		=> $this->input->post('keterangan')
		);
		
		$this->kategori_model->update_kategori($data, $id);
		$this->session->set_flashdata('suc_msg', 'Data berhasil diupdate.');
		redirect('ms_kategori'); 
	}
	
	//Delete
	function delete_kategori()
	{
		$ta_ids_post_array = explode(",",$this->input->post('items'));
		$msg = '';
		foreach($ta_ids_post_array as $index => $ta_id){
			if (isset($ta_id) && $ta_id != "") {
				$this->kategori_model->delete_kategori($ta_id);
				$msg .= "kategori berhasil dihapus.\n";
			}
		}
		$this->output->set_header($this->config->item('ajax_header'));
		$this->output->set_output($msg);
	}
    
}
?>
